// pages/payPage/payPage.js
// pages/payOrder/payOrder.js
var $ = require('../util/commit.js');
var config = require('../util/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    param: '',
    orderID: '',
    orderData: {},
    indexNum: 0,
    user_balance: '',
    payType: [{
      id: '0',
      url: 'https://app.xingxue.vip/icon/icon83.png',
      name: '微信',
    }, {
      id: '1',
      url: 'https://app.xingxue.vip/icon/icon70.png',
      name: '余额',
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let param = JSON.parse(options.data);
    let that = this;
    console.log('-监听页面加载');
    console.log(param);
    that.setData({
      param,
    });
    if (param.page == 'publishActivitie') {
      let orderData = {
        user_nicename: param.title,
        price: param.price,
      }
      this.setData({
        orderID: param.id,
        orderData,
      });
    } else if (param.page == 'collageDetail') {
      let orderData = {
        user_nicename: param.title,
        price: param.price,
      }
      this.setData({
        orderID: param.id,
        orderData,
      });
    }
    that.loadData();


  },
  loadData: function(e) {
    let that = this;
    $.POST({
      url: 'caSUP',
      data: {

      }
    }, function(e) {
      that.setData({
        user_balance: e.data.price
      })
    }, function(e) {})
  },
  // 选择
  choosePayType: function(e) {
    this.setData({
      indexNum: e.currentTarget.dataset.id,
    })
  },
  payBtn: function(e) {
    let self = this;
    if (self.data.param.page == 'publishActivitie') {
      if (self.data.indexNum == 0) {
        // 微信
        $.POST({
          url: 'caCWP',
          data: {
            caid: self.data.orderID,
          }
        }, function(e) {
          wx.requestPayment({
            'timeStamp': e.data.timeStamp,
            'nonceStr': e.data.nonceStr,
            'package': e.data.package,
            'signType': 'MD5',
            'paySign': e.data.paySign,
            'success': function(res) {
              $.openWin({
                url: '../publishActivitie/publishActivitie',
                data: {
                  page: 'payPage'
                }
              })
              wx.showToast({
                title: '支付成功',
              })
            },
            'fail': function(res) {},
            'complete': function(res) {}
          });
        })
      }
      if (self.data.indexNum == 1) {
        $.POST({
          url: 'caCB',
          data: {
            caid: self.data.orderID,
          }
        }, function(e) {

          $.openWin({
            url: '../publishActivitie/publishActivitie',
            data: {
              page: 'payPage'
            }
          })
          wx.showToast({
            title: '支付成功',
          })

        }, function(e) {
          console.log(e);
        })
      }
    } else if (self.data.param.page == 'collageDetail') {
      if (self.data.indexNum == 0) {
        // 微信
        $.POST({
          url: 'caSUCW',
          data: {
            cuid: self.data.orderID,
          }
        }, function (e) {
          wx.requestPayment({
            'timeStamp': e.data.timeStamp,
            'nonceStr': e.data.nonceStr,
            'package': e.data.package,
            'signType': 'MD5',
            'paySign': e.data.paySign,
            'success': function (res) {
              $.openWin({
                url: '../tipsPage/tipsPage',
                data: {
                  title: '参加成功'
                }
              })
              wx.showToast({
                title: '支付成功',
              })
            },
            'fail': function (res) { },
            'complete': function (res) { }
          });
        })
      }
      if (self.data.indexNum == 1) {
        $.POST({
          url: 'caCUB',
          data: {
            cuid: self.data.orderID,
          }
        }, function (e) {

          $.openWin({
            url: '../tipsPage/tipsPage',
            data: {
              title: '参加成功'
            }
          })
          wx.showToast({
            title: '支付成功',
          })

        }, function (e) {
          console.log(e);
        })
      }
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})